package io.renren.modules.blog.controller;

import io.renren.common.annotation.LogOperation;
import io.renren.common.exception.ErrorCode;
import io.renren.common.utils.Result;
import io.renren.common.validator.AssertUtils;
import io.renren.common.validator.ValidatorUtils;
import io.renren.common.validator.group.DefaultGroup;
import io.renren.modules.blog.dto.BlogRouterDto;
import io.renren.modules.blog.service.BlogRouterService;
import io.renren.modules.security.user.SecurityUser;
import io.renren.modules.security.user.UserDetail;
import io.renren.modules.sys.dto.SysMenuDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 博客前端路由
 */
@RestController
@RequestMapping("/blog/router")
@AllArgsConstructor
@Api(tags = "博客路由管理")
public class BlogRouterController {
    private final BlogRouterService blogRouterService;

    @GetMapping("list")
    @ApiOperation("列表")
    @ApiImplicitParam(name = "menuType", value = "菜单类型 0：菜单 1：按钮  null：全部", paramType = "query", dataType = "int")
    public Result<List<BlogRouterDto>> list(Integer menuType) {
        UserDetail user = SecurityUser.getUser();
        List<BlogRouterDto> list = blogRouterService.getAllBlogRouterList(menuType);
        return new Result<List<BlogRouterDto>>().ok(list);
    }

    @GetMapping("menu")
    @ApiOperation("博客前端免登录列表")
    public Result<List<BlogRouterDto>> menu(Integer menuType) {
        UserDetail user = SecurityUser.getUser();
        List<BlogRouterDto> list = blogRouterService.getAllBlogRouterList(0);
        return new Result<List<BlogRouterDto>>().ok(list);
    }


    @GetMapping("select")
    public Result<List<BlogRouterDto>> select() {
        UserDetail user = SecurityUser.getUser();
        List<BlogRouterDto> list = blogRouterService.getBlogRouterList(user, null);
        return new Result<List<BlogRouterDto>>().ok(list);
    }

    @GetMapping
    @ApiOperation("详情")
    @LogOperation("保存")
    public Result<BlogRouterDto> get(@RequestParam("id") Long id) {
        BlogRouterDto data = blogRouterService.get(id);
        return new Result<BlogRouterDto>().ok(data);
    }

    @PostMapping
    @ApiOperation("保存")
    @LogOperation("保存")
    public Result save(@RequestBody BlogRouterDto dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, DefaultGroup.class);

        blogRouterService.save(dto);

        return new Result();
    }

    @PutMapping
    @ApiOperation("修改")
    @LogOperation("修改")
    public Result update(@RequestBody BlogRouterDto dto) {
        //效验数据
        ValidatorUtils.validateEntity(dto, DefaultGroup.class);

        blogRouterService.update(dto);

        return new Result();
    }

    @DeleteMapping("{id}")
    @ApiOperation("删除")
    @LogOperation("删除")
    public Result delete(@PathVariable("id") Long id) {
        //效验数据
        AssertUtils.isNull(id, "id不能为空");

        //判断是否有子菜单或按钮
        List<BlogRouterDto> list = blogRouterService.getListByPid(id);
        if (list.size() > 0) {
            return new Result().error(ErrorCode.SUB_MENU_EXIST, "该菜单存在下级菜单或按钮不允许删除");
        }

        blogRouterService.delete(id);

        return new Result();
    }


}
